import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { getToken } from '@/utils/constant';
import { useUserStore } from '@/store/user';

const routes: Array<RouteRecordRaw> = [
    {
        path: '/login',
        name: 'LoginPage',
        meta: {
            title: '登录',
            keepAlive: true,
            requireAuth: false
        },
        component: () => import('@/pages/login/index.vue')
    },
    {
        path: '/blog-edit/:id',
        name: 'BlogEdit',
        meta: {
            title: '写文章',
            keepAlive: true,
            requireAuth: false
        },
        component: () => import('@/pages/blog/edit/index.vue')
    },
    {
        path: '/gpt',
        name: 'Gpt',
        meta: {
            title: 'gpt聊天页面',
            keepAlive: true,
            requireAuth: true
        },
        component: () => import('@/pages/gpt/index.vue')
    },
    {
        path: '/',
        name: 'Index',
        meta: {
            title: '首页',
            keepAlive: true,
            requireAuth: false
        },
        component: () => import('@/pages/blog/index.vue'),
        children: [
            {
                path: '/',
                name: 'BlogContext',
                component: () => import('@/pages/blog/context/context.vue')
            },
            {
                path: '/show/:id',
                name: 'BlogShow',
                component: () => import('@/pages/blog/show/index.vue')
            },
            {
                path: '/user/setting',
                name: 'UserSetting',
                component: () => import('@/pages/blog/user/setting/index.vue'),
                children: [
                    {
                        path: '/user/setting/info',
                        name: 'SettingUserInfo',
                        component: () => import('@/pages/blog/user/setting/info/index.vue')
                    }
                ]
            },
            {
                path: '/user',
                name: 'UserIndex',
                component: () => import('@/pages/blog/user/index.vue'),
                children: [
                    {
                        path: '/user/show',
                        name: 'UserShow',
                        component: () => import('@/pages/blog/user/show/index.vue')
                    },
                    {
                        path: '/user/collect',
                        name: 'UserCollect',
                        component: () => import('@/pages/blog/user/collect/index.vue')
                    },
                    {
                        path: '/user/column',
                        name: 'UserColumn',
                        component: () => import('@/pages/blog/user/column/index.vue')
                    },
                    {
                        path: '/user/focus',
                        name: 'UserFocus',
                        component: () => import('@/pages/blog/user/focus/index.vue')
                    },
                    {
                        path: '/user/kudos',
                        name: 'UserKudos',
                        component: () => import('@/pages/blog/user/show/index.vue')
                    },
                    {
                        path: '/user/writings',
                        name: 'UserWritings',
                        component: () => import('@/pages/blog/user/writings/index.vue')
                    }
                ]
            }
        ]
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

//添加路由拦截器
router.beforeEach(async (to, from, next) => {
    //查看用户信息是否存在 不存在就去加载用户信息 出
    const userInfo = useUserStore();
    console.log('用户信息：', userInfo.id, userInfo);
    if (getToken() && !userInfo.id) {
        await userInfo.getUserInfo();
    }
    //判断是否需要认证
    if (to.meta.requireAuth) {
        //查看是否有token
        if (!getToken()) {
            return router.push({
                path: '/login'
            });
        }
        next();
    } else {
        next();
    }
});
export default router;
